<template>
  <div class="dashboard-container">
    <div class="app-container">
      <page-tools>
        <template #after>
          <el-button type="primary" @click="$refs.permissionDialog.open(1,'0')">新建权限</el-button>
        </template>
      </page-tools>
      <el-table :data="permissionList" style="width: 100%" row-key="id">
        <el-table-column prop="name" label="权限名称" />
        <el-table-column prop="description" label="描述" />
        <el-table-column prop="code" label="权限标识" />
        <el-table-column v-slot="{row}" label="操作">
          <el-button v-if="row.type===1" type="text" @click="$refs.permissionDialog.open(2,row.id)">新增</el-button>
          <el-button type="text" @click="$refs.permissionDialog.edit(row.id)">编辑</el-button>
          <el-button type="text" @click="del(row.id)">删除</el-button>
        </el-table-column>
      </el-table>
    </div>
    <PermissionDialog ref="permissionDialog" @reload="loadPermissionList" />
  </div>
</template>

<script>
import { getPermissionList, deletePermission } from '@/api/permission'
import { searchSubDepart } from '@/utils/index'
import PermissionDialog from '@/views/permission/component/permissionDialog.vue'
export default {
  components: { PermissionDialog },
  data() {
    return {
      permissionList: []
    }
  },
  created() {
    this.loadPermissionList()
  },
  methods: {
    async loadPermissionList() {
      const res = await getPermissionList()
      this.permissionList = searchSubDepart(res, '0')
    },
    async del(id) {
      await this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
      await deletePermission(id)
      this.$message.success('删除成功')
      this.loadPermissionList()
    }
  }
}
</script>

<style></style>
